package com.example.graduate.Controller;


import com.alibaba.fastjson.JSON;
import com.example.graduate.Bean.*;
import com.example.graduate.Service.CardsCollectService;
import com.example.graduate.Service.ClientService;
import com.fasterxml.jackson.databind.util.JSONPObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
public class CardsController {


    @Autowired
    private CardsCollectService cardsCollectService;

    private List<CardsCollect> userCardList;

    @Autowired
    private ClientService clientService;


    //获取数据库中所有存在卡片集合
    @RequestMapping("/getAllCardsCollect")
    @ResponseBody
    public List<CardsCollect>getAllCardsCollect(){
        List<CardsCollect> userCardList=cardsCollectService.selectAllCards();
        return  userCardList;
    }



    //通过用户id查询该用户拥有哪些卡片
    @RequestMapping("/getUserCardsCollect/{id}")
    @ResponseBody
    public List<CardsCollect> getUserCardsCollect(@PathVariable(name = "id")String id){

        List<CardsCollect> userCardList=cardsCollectService.selectCardsCollect(Integer.valueOf(id));
        Client client=clientService.selectByPrimaryKey(Integer.valueOf(id));
        for(int i=0;i<userCardList.size();i++){
            if(userCardList.get(i).getId()==client.getLearncardsid())
                userCardList.get(i).setChecked(true);
            else
            userCardList.get(i).setChecked(false);
        }
        return  userCardList;
    }

    @GetMapping("/getCardsCollectById/{id}")
    @ResponseBody
    public List<Card>  getCardsCollectByid(@PathVariable(name = "id")  String id ){
        List<Card> list=cardsCollectService.selectCards(Integer.valueOf(id)).getCardList();
        for(int i=0;i<list.size();i++){
            list.get(i).setChecked(false);
        }
        return  list;
    }


    @GetMapping("/getCardById/{id}")
    @ResponseBody
    public Card  getCardById(@PathVariable(name = "id")  String id ){

        return cardsCollectService.getCardById(Integer.valueOf(id));

    }

    @PostMapping("/updateCardById")
    @ResponseBody
    public  void addCardCollect(@RequestBody Card card){


       cardsCollectService.updateCardBy(card);

    }





    @PostMapping("/addCardCollect")
    @ResponseBody
    public  void addCardCollect(@RequestBody CardsCollect cardsCollect){
       cardsCollectService.inser(cardsCollect);
    }

    @PostMapping("/addCard/{cards}/{cardsId}")
    @ResponseBody
    public  void addCard(@PathVariable(name = "cards") String cards,@PathVariable(name = "cardsId")  String cardsId ){

        Integer a=1;

        Card c=  JSON.parseObject(cards,Card.class);
        cardsCollectService.insertCard(c);
        CardsCard cardsCard=new CardsCard();
        cardsCard.setCardid(c.getId());
        cardsCard.setCardsid(Integer.valueOf(cardsId));
        cardsCollectService.insertCardsCard(cardsCard);

    }

    @RequestMapping ("/addPost")
    @ResponseBody
    public  void addPost(@RequestBody Map<String, String> map){
        Post post =new Post();
        String abstrat11;
        post.setTitle((String) map.get("title"));
        post.setType(Integer.valueOf(map.get("type"))+1);
        SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date(System.currentTimeMillis());
        post.setTime(date);
        post.setCreatBy(Integer.valueOf(map.get("creatBy")));
        String content= (String) map.get("content");
        if(content.length()>=10){
        abstrat11=content.substring(0,10);}
        else {
            abstrat11=content;
        }
        post.setContent((String)map.get("content"));
        post.setAbstract11(abstrat11);
        clientService.insertSelective(post);
    }





    @PostMapping("/updateLearnCards/{id}")
    @ResponseBody
    public  void updateLearnCards(@PathVariable(name = "id")  String id ){

        Client client=new Client();
        client.setLearncardsid(Integer.valueOf(id));
        client.setId(1);
        //每次用户更换学习卡片的时候就会把该用户当前学习卡片的序号从0开始
        client.setLearnindex(0);
        cardsCollectService.updateLearnCards(client);

    }

    @PostMapping("/deleteCard/{id}")
    @ResponseBody
    public  void deleteCard(@PathVariable(name = "id")  String id ){


        cardsCollectService.deleteCardById(Integer.valueOf(id));

    }

    @RequestMapping("/deleteCards/{id}/{userId}")
    @ResponseBody
    public  void deleteCards(@PathVariable(name = "id")  String id,@PathVariable(name = "userId")String userId ){


        cardsCollectService.deleteCardsById(Integer.valueOf(id), Integer.valueOf(userId));

    }















}
